//
//  MagazineDetailViewController.m
//  BeautyClother
//
//  Created by dllo on 15/9/1.
//  Copyright (c) 2015年 宋爽. All rights reserved.
//

#import "MagazineDetailViewController.h"
#import "AsyncConnection.h"
#import "define.h"
#import "UIImageView+WebCache.h"
#import "StoreLayout.h"
#import "womenDetailViewController.h"
#import "MJRefresh.h"
#import "back.h"
int j = 0;

@interface MagazineDetailViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) NSMutableArray *flagArr;
@property (nonatomic, copy) NSString *flag;

@property (nonatomic, assign) CGFloat offWidth;
@property (nonatomic, assign) CGFloat offHeight;

@end

@implementation MagazineDetailViewController

- (void)dealloc
{
    [_collection release];
    [_array release];
    [_flagArr release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.flagArr = [NSMutableArray array];
        self.flagArr = @[@"2015-07-27%2016%3A03%3A02", @"2015-05-25%2018%3A25%3A57"].mutableCopy;
        
        self.offWidth = [[UIScreen mainScreen]bounds].size.width / 375;
        self.offHeight = [[UIScreen mainScreen]bounds].size.height / 667;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self BeginConnection];
    back *fanhui = [[back alloc]initWithImage:[UIImage imageNamed:@"f.png"] style:UIBarButtonItemStyleDone target:self action:@selector(fanhui:)];
    
    self.navigationItem.leftBarButtonItem = fanhui;
    // Do any additional setup after loading the view.
}

- (void)fanhui:(back *)ba
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

/// 加载刷新
- (void)addHeader
{
    __block MagazineDetailViewController *actionVC = self;
    
    [self.collection addHeaderWithCallback:^{
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [actionVC.collection headerEndRefreshing];
                    });
        [self.collection headerEndRefreshing];
    }];
    
    
}
- (void)addFooter
{
    
    if (j <= 1) {
        
        
    __block MagazineDetailViewController *actionVC = self;
    
    [self.collection addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
            NSDictionary *dic = @{@"tag_id":@"14",
                                  
                                  @"flag":[self.flagArr objectAtIndex:j]};
            
            
            [AsyncConnection asyncConnection:MAGAZINE parmaters:dic block:^(NSData *data) {
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                
                NSMutableArray *arr = [NSMutableArray array];
                
                arr = [[dic objectForKey:@"data"]objectForKey:@"items"];
            
                [self.array addObjectsFromArray:arr];
                
              
                
                [self.collection reloadData];
                
            
            }path:@"magazine_2"];
            
           j++;
           
            [actionVC.collection footerEndRefreshing];
        });
        
    }];
        
    }
    
}



- (void)BeginConnection
{
    
    
    NSDictionary *dic = @{@"tag_id":@"14",
                          
                          @"flag":@""};
    
    [AsyncConnection asyncConnection:MAGAZINE parmaters:dic block:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.array = [[dic objectForKey:@"data"]objectForKey:@"items"];
        
       
        [self createCollection];
            }path:@"mazagine"];
}

- (void)createCollection
{
    
    StoreLayout *layout = [[StoreLayout alloc]init];
    layout.dicArray = self.array;
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:layout];
    [self.view addSubview:self.collection];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    self.collection.backgroundColor = [UIColor whiteColor];
    [self.collection release];
    
    [self addHeader];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.array.count <= 44) {
        
        [self addFooter];
    }
    return self.array.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20 *_offHeight, 180 *_offWidth, 250 *_offHeight)];
    [cell.contentView addSubview:image];
//    NSDictionary *dic = [[self.array objectAtIndex:indexPath.item]objectForKey:@"component"];
    NSString *pic = [[[self.array objectAtIndex:indexPath.item]objectForKey:@"component"]objectForKey:@"picUrl"];
    NSArray *arr = [pic componentsSeparatedByString:@"?"];
    
    [image sd_setImageWithURL:[NSURL URLWithString:[arr objectAtIndex:0]] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
    
    
    
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    womenDetailViewController *maga = [[womenDetailViewController alloc]init];
    NSString *str = [@"http://api2.hichao.com/theme?id=" stringByAppendingString:[[[[self.array objectAtIndex:indexPath.item]objectForKey:@"component"]objectForKey:@"action"]objectForKey:@"id"]];
    NSURL *url = [NSURL URLWithString:str];
    maga.url = url;
    [self.navigationController pushViewController:maga animated:YES];
    [maga release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
